package jp.co.technotree.common.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import jp.co.technotree.common.controll.ApplicationContext;
import jp.co.technotree.common.util.DBGLog;
import jp.co.technotree.exception.TransactionException;

public class TransactionManager {
	private static String CLASSNAME=TransactionManager.class.getName();
	private static ThreadLocal thlocal = new ThreadLocal();
	
	public TransactionManager(){
		
	}
	
	public static Transaction getTransaction() throws TransactionException{
		Object object = thlocal.get();
		Transaction trx = null;
		
		if ( object == null ){
			Connection conn = getConnection();
			trx = new Transaction(conn);
			thlocal.set(trx);
		}else{
			trx = (Transaction) object;
			
		}
		
		return trx;
	}
	
	public static Transaction getPrivateTransaction() throws TransactionException{
		Transaction trx = null;
		Connection conn = getConnection();
		trx = new Transaction(conn);
		
		return trx;
	}

	public static void releaseTransaction(){
		Object object = thlocal.get();
		Transaction trx = null;
		if ( object == null ){
		}else{
			trx = (Transaction) object;
			Connection conn = trx.getConnection();
			try {
				releaseConnection(conn);
			} catch (TransactionException e) {
				e.printStackTrace();
			}
			trx = null;
			thlocal.set(null);
		}
		
	}
	
	protected static Connection getConnection() throws TransactionException{
		String METHODNAME = "getConnection";
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"START");

		try {
			//String dsn = "jdbc:postgresql://127.0.0.1:5432/postgres?user=postgres&password=nPxjk13";
			
			ApplicationContext context = ApplicationContext.getInstance();
			
			String url = context.getConfig("database.url");
			String user = context.getConfig("database.user");
			String password = context.getConfig("database.password");
			String driver = context.getConfig("database.driver");
			DBGLog.fwdebug(CLASSNAME,METHODNAME,"database.url:"+url);
			DBGLog.fwdebug(CLASSNAME,METHODNAME,"database.user:"+user);
			DBGLog.fwdebug(CLASSNAME,METHODNAME,"database.password:"+password);
			DBGLog.fwdebug(CLASSNAME,METHODNAME,"database.driver:"+driver);
			
			Class.forName(driver);
			
			//Connection con = DriverManager.getConnection(dsn); // PostgreSQLの場合
			Connection con = DriverManager.getConnection(url, user, password);

			con.setAutoCommit(false);

			DBGLog.fwdebug(CLASSNAME,METHODNAME,"con:"+con);

			DBGLog.fwdebug(CLASSNAME,METHODNAME,"END");
			return con;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new TransactionException("トランザクション取得エラー");
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TransactionException("トランザクション取得エラー");
		} 
	}
	
	protected static void releaseConnection(Connection conn) throws TransactionException{
		String METHODNAME = "releaseConnection";
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"START");

		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new TransactionException("トランザクション開放エラー");
		}
		
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"END");
	}
	
	
}
